import { defineStore } from "pinia";
import { getUserInfo } from "@/utils/api/login";
import { useGlobalMenuStore } from "./menuGlobalStore";
export const useGlobalUserStore = defineStore("global-user", {
  state: () => {
    return {
      userInfo: null,
      roles: [], //角色信息
      currentRolePerm: null, //角色权限编码
      permissions: [], //权限信息
      units: [], //单位信息
    };
  },
  getters: {},
  actions: {
    //获取用于信息
    async getGlobalUserInfo() {
      if (!this.userInfo) {
        const res = await getUserInfo();
        let { userInfo, permissions, roles, units } = res.data;        
        this.permissions = permissions;
        this.roles = roles; //roles:[{id:"0",roleName:"超级管理员",rolePerm:"ROLE_ADMIN"}]
        this.userInfo = userInfo;
        this.units = units;
        //把角色权限编码存到sessionStorage中，方便获取路由表传递参数
        if (!this.currentRolePerm) {
          this.currentRolePerm = roles[0].rolePerm; 
          sessionStorage.setItem("currentRolePerm", roles[0].rolePerm);
        }
        await useGlobalMenuStore().getMenuTree();
      }
    },
  },
});
